package com.shareppy.normanapitest.lexic;

import java.util.ArrayList;
import java.util.List;

import com.shareppy.normanapitest.lexic.lexemes.LexemeState;
import com.shareppy.normanapitest.lexic.lexemes.StartLexeme;

public class LexicalAnalyzer {

	private LexemeCreator lexemeCreator = new LexemeCreator();
	private List<Lexeme> list = new ArrayList<Lexeme>();

	public void analize(String program) {
		Lexeme current = new StartLexeme();
		char[] charArray = program.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			char ch = charArray[i];
			LexemeState lexemeState = current.supports(ch);
			if (lexemeState == LexemeState.CONTINUE) {
				current.append(ch);
			} else if (lexemeState == LexemeState.END_INCLUDE) {
				if (!(current instanceof StartLexeme)) {
					current.append(ch);
					list.add(current);
				}
				current = new StartLexeme();
				continue;
			} else if (lexemeState == LexemeState.END) {
				if (!(current instanceof StartLexeme)) {
					list.add(current);
				}
				current = lexemeCreator.create(ch);
			}
		}
		if (!(current instanceof StartLexeme)) {
			list.add(current);
		}

		for (int i = 0; i < list.size(); i++) {
			System.out.println( list.get(i) );
		}
	}
}